let sock = io.connect("ws://localhost:8089");
var str = null;//聊天拼接字符串;
function penStr(imgurl,txt,where){
	return (
		'<dl class="'+where+'">'+
			'<dt class="clearfloat">'+
				'<span class="imgHead" style="background:url('+imgurl+') no-repeat center;background-size:cover;"></span>'+
				'<span class="userName">'+
					'<div class="box">'+
						'<span class="chatsjilu">'+txt+'</span>'+
						'<span class="rightj"></span>'+
						'<span class="rightj"></span>'+
					'</div>'+
				'</span>'+
			'</dt>'+
		'</dl>'
	)
}
//键盘事件
document.onkeydown = function(event){
	var ev = window.event||event;
	if(ev&&ev.keyCode==13){
		sock.emit("msg",$(".txt").val(),username,imgurl);
		str = penStr(imgurl,$(".txt").val(),'rightchat');
	}
};
//发送聊天者的名字
sock.emit("chatname",$(".username").data('name'),$("#zhaopian").attr("src"));
//发送反馈事件
sock.on("msg_ret",(code,msg)=>{
	if(code){
		alert(msg)
	}else{
		$(str).appendTo($(".chatrecoreds"));
		document.querySelector(".chatrecoreds").scrollIntoView(false);
		$(".txt").val('');
	};
});
//接受其他人发送的消息
sock.on("msg",(txt,name,imgUrl)=>{
	str = penStr(imgUrl,txt,'leftchat')
	$(str).appendTo($(".chatrecoreds"));
	document.querySelector(".chatrecoreds").scrollIntoView(false);
});
var username = $(".username").data('name');
var imgurl = $("#zhaopian").attr("src");
//点击发送消息
$(".textinn").click(function(){
	sock.emit("msg",$(".txt").val(),username,imgurl);
	str = penStr(imgurl,$(".txt").val(),'rightchat');
});